﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Xml.Linq;

namespace MassIntellect.WorkIt
{
	public class PulseArgs : IDictionary<string, object>
	{
		private Dictionary<string, object> dict = new Dictionary<string, object>();

		public Guid Id { get; private set; }

		public PulseArgs Clone()
		{
			return new PulseArgs(Sender)
					{
						Id = Id
					}
				.AddParams(dict);
		}

		public PulseArgs(IPipe sender)
		{
			Id = Guid.NewGuid();
			this.Sender = sender;
		}

		public IPipe Sender { get; set; }

		public PulseArgs AddParams(IEnumerable<PulsePrm> prms)
		{
			foreach (var item in prms)
				this.AddOrSetValue(item.Key ?? "", item.Value);
			return this;
		}

		public PulseArgs AddParams(IEnumerable<KeyValuePair<string, object>> prms)
		{
			foreach (var item in prms)
				this.AddOrSetValue(item.Key ?? "", item.Value);
			return this;
		}

		public PulseArgs AddParams(params PulsePrm[] prms)
		{
			return AddParams((IEnumerable<PulsePrm>)prms);
		}

		public PulseArgs AddParam(string key, object obj)
		{
			this.AddOrSetValue(key ?? "", obj);
			return this;
		}



		#region IDictionary<string,object> Members

		public void Add(string key, object value)
		{
			dict.Add(key ?? "", value);
		}

		public bool ContainsKey(string key)
		{
			return dict.ContainsKey(key);
		}

		public ICollection<string> Keys
		{
			get { return dict.Keys; }
		}

		public bool Remove(string key)
		{
			return dict.Remove(key);
		}

		public bool TryGetValue(string key, out object value)
		{
			return dict.TryGetValue(key ?? "", out value);
		}

		public ICollection<object> Values
		{
			get { return dict.Values; }
		}

		public object this[string key]
		{
			get
			{
				return dict.GetOrDefault(key ?? "", null);
			}
			set
			{
				dict.AddOrSetValue(key ?? "", value);
			}
		}

		#endregion

		#region ICollection<KeyValuePair<string,object>> Members

		public void Add(KeyValuePair<string, object> item)
		{
			dict.Add(item.Key ?? "", item.Value);
		}

		public void Clear()
		{
			dict.Clear();
		}

		public bool Contains(KeyValuePair<string, object> item)
		{
			object obj;
			return dict.TryGetValue(item.Key ?? "", out obj) && obj == item.Value;
		}

		public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
		{
			foreach (var item in dict)
				array[arrayIndex++] = item;
		}

		public int Count
		{
			get { return dict.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(KeyValuePair<string, object> item)
		{
			object obj;
			bool res;
			if (res = (dict.TryGetValue(item.Key ?? "", out obj) && obj == item.Value))
				res = dict.Remove(item.Key ?? "");
			return res;
		}

		#endregion

		#region IEnumerable<KeyValuePair<string,object>> Members

		public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
		{
			return dict.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return dict.GetEnumerator();
		}

		#endregion
	}

	public struct PulsePrm
	{
		public string Key;

		public object Value;

		public static PulsePrm Create(string key, object value)
		{
			return new PulsePrm { Key = key ?? "", Value = value };
		}

		public static implicit operator KeyValuePair<string, object>(PulsePrm arg)
		{
			return new KeyValuePair<string, object>(arg.Key ?? "", arg.Value);
		}

		public static implicit operator PulsePrm(KeyValuePair<string, object> arg)
		{
			return Create(arg.Key ?? "", arg.Value);
		}
	}
}
